<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * 短信接口
 * 
 * 示例：
    $target = "http://cf.51welink.com/submitdata/Service.asmx/g_Submit";
    替换成自己的测试账号,参数顺序和wenservice对应
    $post_data = "sname=kwsm&spwd=kwsm&scorpid=&sprdid=1012818&sdst=13910862579&smsg=".rawurlencode("短信内容【签名】");
    $binarydata = pack("A", $post_data);
    echo $gets = Post($post_data, $target);
*/
class ShortMessage{

    private $textTarget = "http://cf.51welink.com/submitdata/Service.asmx/g_Submit";
    private $sname = 'dlfyjz00';
    private $spwd = 'wang123456';
    private $scorpid = '';
    private $sprdid = '1012818';
    public function __construct(){
        $this->CI =& get_instance(); 
    }

    /**
     * [send description]
     * @param  [type] $data   ["短信内容【签名】"]
     * @return [type]         [description]
     */
    public function send($account,$data) {

        $target = $this->textTarget;
        $post_data = "sname=$this->sname&spwd=$this->spwd&scorpid=$this->scorpid&sprdid=$this->sprdid&sdst=$account&smsg=".rawurlencode($data);
        // echo $post_data;exit;
        // $binarydata = pack("A", $post_data);

        //组装http包
        $url_info = parse_url($target);
        $httpheader = "POST " . $url_info['path'] . " HTTP/1.0\r\n";
        $httpheader .= "Host:" . $url_info['host'] . "\r\n";
        $httpheader .= "Content-Type:application/x-www-form-urlencoded\r\n";
        $httpheader .= "Content-Length:" . strlen($post_data) . "\r\n";
        $httpheader .= "Connection:close\r\n\r\n";
        //$httpheader .= "Connection:Keep-Alive\r\n\r\n";
        $httpheader .= $post_data;

        $fd = fsockopen($url_info['host'], 80);
        fwrite($fd, $httpheader);
        $gets = "";
        while(!feof($fd)) {
            $gets .= fread($fd, 128);
        }
        fclose($fd);
        if($gets != ''){
            $start = strpos($gets, '<?xml');
            if($start > 0) {
                $gets = substr($gets, $start);
            }        
        }
        if(strpos($gets,"<State>0</State>")>=0)
            return 0;
        else
            return 2017;
    }
}